package com.gdpu.liou.symphonyserver.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Fox
 * @date 2022/3/9 11:22
 */
@RestController
@RequestMapping("/app")
public class AppController {
    private static final String APP_ID = "wx7c5967955fd47d53";
    private static final String APP_SECRET = "931952f1bbde505b47f33de31f91c7e3";

    @Resource
    private RestTemplate restTemplate;


    @GetMapping("/getOpenId")
    public String getOpenId(@RequestParam("code") String code) {
        try {
            Map<String, String> params = new HashMap<>(5);
            params.put("appid", APP_ID);
            params.put("secret", APP_SECRET);
            params.put("code", code);
            params.put("grant_type", "authorization_code");
            String url = "https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={secret}&js_code={code}&grant_type={grant_type}";
            String response = restTemplate.getForObject(url, String.class, params);
            System.out.println(response);
            return response;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
